<!DOCTYPE html>
<html>
<head>
<head><#include "/base/head.html">
<body>
	<div>
		<div id="resourceToolbar">
			<a id="resourceAdd" href="javascript:void(0)" class="easyui-linkbutton"
				iconcls="icon-add" plain="true">新增</a>
				<a id="resourceAddSub" href="javascript:void(0)" class="easyui-linkbutton"
				iconcls="icon-add" plain="true">新增子节点</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit"
				onclick="editResource()" plain="true">编辑</a>
			<a id="resourceEdit" postUrl="resource/addUpdateUser" class="easyui-linkbutton"
				iconcls="icon-edit" onclick="saveResource()" plain="true">保存</a>
			<a id="resourceEdit" postUrl="resource/addUpdateUser" href="javascript:void(0)"
				class="easyui-linkbutton" iconcls="icon-edit" plain="true" onclick="cancelResource()">撤销</a>
			<a id="resourceDelete" postUrl="resource/deleteUser" href="javascript:void(0)"
				class="easyui-linkbutton" iconcls="icon-remove" plain="true"
				onclick="deleteResource()">删除</a>

		</div>
	</div>

	<script type="text/javascript">
		function deleteResource() {
			if (editingId != undefined) {
				$('#resource_treegrid').treegrid('select', editingId);
				return;
			}

			var row = $('#resource_treegrid').treegrid('getSelected');
			var ok = confirm("是否要删除 id 为 " + row.id + " 的资源?");
			if (!ok)
				return;

			BaseJs.ajax("${contextPath}/sys/resource/deleteResource", {
				id : row.id
			}, function(r) {
				$('#resource_treegrid').treegrid('remove', row.id);
				alert("删除成功");
			});

		}

		var resourceNextId = -1;
		
		$("#resourceAddSub").click(function() {
			if (editingId != undefined) {
				$('#resource_treegrid').treegrid('select', editingId);
				return;
			}
			editingId = " ";
			var node = $('#resource_treegrid').treegrid('getSelected');
			
			if (node) {
				$('#resource_treegrid').treegrid('append', {
					parent : node.id,
					data : [{ 
						id : " ",
						resource : "",
						description : "",
						status : ""
					}]
				});
			}
			$('#resource_treegrid').treegrid('beginEdit', " ");
			
		});
		
		$("#resourceAdd").click(function() {
			if (editingId != undefined) {
				$('#resource_treegrid').treegrid('select', editingId);
				return;
			}
			editingId = " ";
			var node = $('#resource_treegrid').treegrid('getSelected');
			if (node) {
				$('#resource_treegrid').treegrid('insert', {
					after : node.id,
					data : { 
						id : " ",
						resource : "",
						description : "",
						status : ""
					}
				});
			}
			$('#resource_treegrid').treegrid('beginEdit', " ");
		});

		var editingId;

		function editResource() {
			if (editingId != undefined) {
				$('#resource_treegrid').treegrid('select', editingId);
				return;
			}
			var row = $('#resource_treegrid').treegrid('getSelected');
			if (row) {
				editingId = row.id
				$('#resource_treegrid').treegrid('beginEdit', editingId);
			}
		}

		function saveResource() {
			if (editingId != undefined) {
				var t = $('#resource_treegrid');
				$('#resource_treegrid').treegrid('select', editingId);
				var row = t.treegrid('getSelected');
				delete row.children;
				t.treegrid('endEdit', editingId);
				$('#resource_treegrid').treegrid('beginEdit', editingId);
				if (row.id == " ") {

					row = t.treegrid('getSelected');
					console.log(row);
					row.pid = row._parentId;
					
					BaseJs.ajax("${contextPath}/sys/resource/addResource", row, function(r) {
						$('#resource_treegrid').treegrid('update',{
							id : " ",
							row: {
								id: r.data
							}
						});
						t.treegrid('endEdit', editingId);

						editingId = undefined;

					});
				} else {
					BaseJs.ajax("${contextPath}/sys/resource/updateResource", row, function(r) {
						console.log(r);
						t.treegrid('endEdit', editingId);
						editingId = undefined;
					});
				}
				
				$('#resource_treegrid').treegrid('reload');
			}
		}
		function cancelResource() {
			if (editingId != undefined) {
				if(editingId == " ")
					$('#resource_treegrid').treegrid('remove', " ");
				else
					$('#resource_treegrid').treegrid('cancelEdit', editingId);
				
				editingId = undefined;
			}
		}
	</script>

	<table title="Folder Browser" id="resource_treegrid" class="easyui-treegrid"
		style="width: 700px; height: 250px"
		data-options="
                url: '${contextPath}/sys/resource/queryResourcesTree',
                method: 'get',
                rownumbers: true,
                idField: 'id',
                treeField: 'description',
                         animate: true,
                collapsible: true,
                onDblClickRow : editResource">
		<thead>
			<tr>
				<th data-options="field:'description',editor:'textbox'" width="150">描述</th>
				<th data-options="field:'id'" width="120">ID</th>
				<th data-options="field:'resource',editor:'textbox'" width="300">资源</th>
				<th data-options="field:'status',editor:'textbox'" width="150">状态</th>
			</tr>
		</thead>
	</table>
</body>
</html>